/* eslint-env node */
const path = require('path')

const webpack           = require('webpack')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const fs                = require('fs-extra')
const config            = require('config')
const jsonfile          = require('jsonfile')


webpack({
	output: {
		filename: '[name].js',
		library : '[name]',
		path    : path.resolve('./dist/'),
	},
	entry: {
		dll: [
			config.get('webpack.resolve.alias.bootstrap'),
			config.get('webpack.resolve.alias.vue'),
			config.get('webpack.resolve.alias.axios'),
			config.get('webpack.resolve.alias.lodash'),
			config.get('webpack.resolve.alias.vue-router'),
			config.get('webpack.resolve.alias.filesize'),
			config.get('webpack.resolve.alias.moment'),
			config.get('webpack.resolve.alias.vuex'),
			config.get('webpack.resolve.alias.bluebird'),
			config.get('webpack.resolve.alias.font-awesome'),
			config.get('webpack.resolve.alias.store'),
		],
	},
	module: {
		loaders: [
			{
				test   : /\.(woff|woff2|eot|ttf|svg)/,
				loaders: ['file-loader?name=[path][name].[ext]'],
			},
			{
				test  : /\.css$/,
				loader: ExtractTextPlugin.extract('style-loader', 'css-loader'),
			},
			{
				test   : /(moment|filesize|setimmediate|bluebird)\/.*/,
				loaders: ['babel-loader'],
			},
		],
	},
	plugins: [
		new webpack.DllPlugin({
			path   : './dist/manifest.json',
			name   : '[name]',
			context: __dirname,
		}),
		new webpack.ContextReplacementPlugin(/moment\/locale$/, /zh-cn/),
		new ExtractTextPlugin('styles.css'),
	],
}).run((err, stats) => {
	console.log(stats.toString('normal'))


	const file = './dist/stats/dll.json'

	fs.ensureFileSync(file)

	jsonfile.writeFileSync(file, stats.toJson())
})